package com.hmkj.admin.controller.blackList;

import com.hmkj.admin.controller.BaseController;
import com.hmkj.admin.controller.GenericController;
import com.hmkj.common.entity.ResultEntity;
import com.hmkj.common.entity.dtgrid.DtGrid;
import com.hmkj.common.utils.PingYinUtil;
import com.hmkj.common.utils.StringUtils;
import com.hmkj.core.constant.CacheID;
import com.hmkj.core.eolinker.endpoints.QuotationEndpoint;
import com.hmkj.core.eolinker.models.ModelRealTime;
import com.hmkj.core.eolinker.models.StockListModel;
import com.hmkj.core.exception.BussinessException;
import com.hmkj.core.po.blackList.BlackList;
import com.hmkj.core.service.blackList.BlackListService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping(BaseController.BASE_URI)
public class BlackListJsonController extends GenericController<Integer, BlackList> {
    @Resource
    private BlackListService blackListService;

    /**
     * 策略排行
     *
     * @param dtGridPager
     * @return
     */
    @ResponseBody
    @RequestMapping("blackList/json/list")
    public DtGrid userListJson(String dtGridPager) throws Exception {
        return blackListService.getDtGridList(dtGridPager, BlackList.class);
    }


    /**
     * 股票信息json数据
     *
     * @param stockCode
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/stock/list.json", method = RequestMethod.POST)
    public ResultEntity stockListJson(String stockCode) {
            List<StockListModel> resultList = new ArrayList<>();
        if (StringUtils.isNotBlank(stockCode)) {
            //上证
            stockCode = stockCode.trim();
            List<StockListModel> upList = redisService.getList(CacheID.STOCK_LIST_PREFIX, StockListModel.class);
            for (StockListModel upStock : upList) {
                if (upStock.getCode_name().contains(stockCode) || upStock.getStock_code().contains(stockCode) || PingYinUtil.getFirstSpell(upStock.getCode_name()).contains(stockCode.trim().toUpperCase())) {
                    resultList.add(upStock);
                }
            }
        }
        return ok(resultList);
    }

    /**
     * 新增
     *
     * @param
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping("blackList/json/add")
    @RequiresPermissions("sys:black:save")
    public ResultEntity addJson(BlackList blackList) throws Exception {
        checkModel(blackList);
        if (blackListService.selectCount(new BlackList(t -> {
            t.setStockCode(blackList.getStockCode());
            t.setDelFlag(BlackList.DELFLAG.NORMAL.code);
        })) > 0 || blackListService.selectCount(new BlackList(t -> {
            t.setStockName(blackList.getStockName());
            t.setDelFlag(BlackList.DELFLAG.NORMAL.code);
        })) > 0) {
            throw new BussinessException("股票名称或股票代码已存在");
        }
        //获取股票信息
        ModelRealTime modelRealTime = QuotationEndpoint.realtime(blackList.getStockCode());
        if(null == modelRealTime){
            throw new BussinessException("股票不存在");
        }
        blackListService.insert(blackList);
        return ok();
    }

    /**
     * 编辑股票黑名单
     */
    @ResponseBody
    @RequestMapping("blackList/json/edit")
    @RequiresPermissions("sys:black:edit")
    public ResultEntity editBlackList(BlackList blackList) throws Exception {
        BlackList blackList1 = checkPrimaryKey(blackList.getId());
        checkModel(blackList);
        if (!blackList1.getStockCode().equals(blackList.getStockCode()) || !blackList1.getStockName().equals(blackList.getStockName())){
            if (blackListService.selectCount(new BlackList(t -> {
                t.setStockName(blackList.getStockName());
                t.setDelFlag(BlackList.DELFLAG.NORMAL.code);
            })) > 0 || blackListService.selectCount(new BlackList(t -> {
                t.setStockCode(blackList.getStockCode());
                t.setDelFlag(BlackList.DELFLAG.NORMAL.code);
            })) > 0) {
                throw new BussinessException("股票代码或股票名称已存在");
            }
        }
        //获取股票信息
        ModelRealTime modelRealTime = QuotationEndpoint.realtime(blackList.getStockCode());
        if(null == modelRealTime){
            throw new BussinessException("股票不存在");
        }
        blackListService.updateByPrimaryKeySelective(blackList);
        return ok();
    }

    /**
     * 删除
     *
     * @param id
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value = "blackList/json/del")
    @RequiresPermissions("sys:black:del")
    public ResultEntity del(Integer id) throws Exception {
        BlackList blackList=checkPrimaryKey(id);
        blackListService.updateByPrimaryKeySelective(new BlackList(a -> {
            a.setId(blackList.getId());
            a.setDelFlag(BlackList.DELFLAG.DELETED.code);
        }));
        return ok();
    }

    private void checkModel(BlackList blackList) {
        if (StringUtils.isBlank(blackList.getStockName())) {
            throw new BussinessException("股票名称不能为空");
        } else if (blackList.getStockName().length() > 30) {
            throw new BussinessException("股票名称长度不能超过30位");
        } else if (StringUtils.isBlank(blackList.getStockCode())) {
            throw new BussinessException("股票代码不能为空");
        } else if (blackList.getStockCode().length() > 30) {
            throw new BussinessException("股票代码长度不能超过30位");
        }
    }

    private BlackList checkPrimaryKey(Integer id) throws Exception {
        if (null == id) {
            throw new BussinessException("参数有误");
        } else {
            BlackList blackList = blackListService.selectByPrimaryKey(id);
            if (null != blackList) {
                return blackList;
            } else {
                throw new BussinessException("参数有误");
            }
        }
    }

}
